package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.vectrics.company.CompanyPersonGroup;
import org.vectrics.employee.Employee;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.user.Person;

import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.UserListReportDTO;
import com.hexacta.hrs.services.candidate.CandidateService;
import com.hexacta.hrs.services.employee.EmployeeService;

/**
 * @author MMicheletto clase que genera el reporte de usuarios
 */
public class UserListReport extends AbstractReport {
    /** {@inheritDoc} */

    private CandidateService candidateService;

    private EmployeeService employeeService;

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public CandidateService getCandidateService() {
        return candidateService;
    }

    public void setCandidateService(final CandidateService candidateService) {
        this.candidateService = candidateService;
    }

    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /** {@inheritDoc} */
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection getReportData(final Map<String, Object> filters) {

        Collection<UserListReportDTO> collection = new ArrayList<UserListReportDTO>();
        Collection<CandidateInfo> collectionCandidate = candidateService.getAll();
        Collection<Employee> collectionEmployee = employeeService.getAll();
        Collection<Person> perCollectionCandidate = CollectionUtils.collect(collectionCandidate, new Transformer() {
            @Override
            public Object transform(final Object input) {
                CandidateInfo candidate = (CandidateInfo) input;
                return candidate.getPerson();
            }
        });

        collectionEmployee.removeAll(employeeService.getNoLongerEmployees());
        for (Employee employee : collectionEmployee) {
            UserListReportDTO userListReportDTO = new UserListReportDTO();
            userListReportDTO.setFirstName(employee.getPerson().getFirstName());
            userListReportDTO.setLastName(employee.getPerson().getLastName());
            if (employee.getPerson().getUser() != null) {
                userListReportDTO.setUserName(employee.getPerson().getUser().getUserName());
            } else {
                userListReportDTO.setUserName("------");
            }
            // Si esta como candidato, lo saco para despues no listarlo. Y ya se
            // que es employee candidato
            if (perCollectionCandidate.contains(employee.getPerson())) {
                perCollectionCandidate.remove(employee.getPerson());

            }
            userListReportDTO.setPersonType("Employee");

            userListReportDTO.setRoles(makeRolesSummary(employee.getPerson()));
            collection.add(userListReportDTO);
        }

        for (Person person : perCollectionCandidate) {
            UserListReportDTO userListReportDTO = new UserListReportDTO();
            userListReportDTO.setFirstName(person.getFirstName());
            userListReportDTO.setLastName(person.getLastName());
            if (person.getUser() != null) {
                userListReportDTO.setUserName(person.getUser().getUserName());
            } else {
                userListReportDTO.setUserName("------");
            }
            userListReportDTO.setPersonType("Candidate");
            userListReportDTO.setRoles(makeRolesSummary(person));
            collection.add(userListReportDTO);
        }

        if (collection.size() == 0)
            throw new RuntimeException("No data Available for Report");
        Collections.sort((List) collection);
        return collection;

    }

    @SuppressWarnings("unchecked")
    private String makeRolesSummary(final Person person) {
        String roleSummary = "";
        List companyGroups = CoreServiceLocator.getPersonService().findCompanyGroupsForPerson(person);
        Iterator iter = companyGroups.iterator();
        while (iter.hasNext()) {
            CompanyPersonGroup group = (CompanyPersonGroup) iter.next();
            String groupName = getPhrase(group.getCompanyPersonGroupType().getDescriptionCode());
            roleSummary += groupName;
            roleSummary += " ";
        }

        if (roleSummary.length() == 0) {
            roleSummary = "----";
        }

        return roleSummary;

    }

    public String getPhrase(final String code) {
        Locale locDesc = new Locale("es");
        return CoreServiceLocator.getPhraseProvider().getMessage(code, locDesc);
    }

    /** {@inheritDoc} */
    @Override
    @SuppressWarnings("unchecked")
    protected Map getReportParameters() {
        // TODO Auto-generated method stub
        return null;
    }

}

// }
